﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Bankomat.Business
{
    public class OperationManager : IOperationManager  //Singleton
    {
        readonly private int[] BalanceValues = new int[4] { 10000, 20000, 50000, 100000 };
        private int balance;

        private int tens;
        private int fives;
        private int ones;

        private static OperationManager instance;

        private OperationManager()
        {
            var notes = new Dictionary<Banknote, int>
            {
                { Banknote.Thousand, 10 },
                { Banknote.FiveThousand, 2 },
                { Banknote.TenThousand, 1}
            };

            Casettes = new Casettes(notes);
        }

        public static OperationManager Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new OperationManager();
                }
                return instance;
            }
        }

        public Casettes Casettes { get; private set; }

        public int GetTens
        {
            get
            {
                return tens;
            }
        }

        public int GetFives
        {
            get
            {
                return fives;
            }
        }

        public int GetOnes
        {
            get
            {
                return ones;
            }
        }

        public int GetBalance()
        {
            return balance;
        }

        public void ResetBalance()
        {
            Random r = new Random();
            balance = BalanceValues[r.Next(0, 4)];
        }

        public WithDrawResult Withdraw(int sum)
        {
            if (Casettes == null)
            {
                MessageBox.Show("Casettes is not set");
                return null;
            }

            int casettesOnes = Casettes.Notes[Banknote.Thousand];
            int casettesFives = Casettes.Notes[Banknote.FiveThousand];
            int casettesTens = Casettes.Notes[Banknote.TenThousand];

            if(GetBalance() < sum)
            {
                return new WithDrawResult { ResultEnum = WithDrawResultEnum.UnSuccess };
            }
            
            tens = sum / 10000;
            int rest1 = sum % 10000;
            if (tens > casettesTens)
            {
                rest1 += (tens - casettesTens) * 10000;
                tens = casettesTens;
                Casettes.DecreaseNotes(Banknote.TenThousand, casettesTens);
            }
            else
            {
                Casettes.DecreaseNotes(Banknote.TenThousand, tens);
            }
            

            fives = rest1 / 5000;
            int rest2 = rest1 % 5000;
            if(fives > casettesFives)
            {
                rest2 += (fives - casettesFives) * 5000;
                fives = casettesFives;
                Casettes.DecreaseNotes(Banknote.FiveThousand, casettesFives);
            }
            else
            {
                Casettes.DecreaseNotes(Banknote.FiveThousand, fives);
            }

            ones = rest2 / 1000;
            if (ones > casettesOnes)
            {
                return new WithDrawResult { ResultEnum = WithDrawResultEnum.EmptyCasette };
            }
            else
            {
                Casettes.DecreaseNotes(Banknote.Thousand, ones);
            }

            balance -= sum;

            var result = new Dictionary<Banknote, int>();

            result.Add(Banknote.TenThousand, tens);
            result.Add(Banknote.FiveThousand, fives);
            result.Add(Banknote.Thousand, ones);

            return new WithDrawResult { Notes = result };  
        }
    }
}
